<?php

namespace ball;
use GuzzleHttp\Client;
use think\facade\Cache;

/**
 * 金叶照明物联控制api
 */
class JinyeLightIot
{
    protected $url = 'https://api-light.georgdq.com';
    protected array $config = [
        'appId'     => 'ttb8e5fe87570cb5a801', // 
        'appSecret' => '2abebcd0d427297fd254863d05a2554c46145407', // 
    ];
    protected $errMsg = '';
    protected int $refreshTokenKeepTime = 7199;

    /**
     * 构造方法
     * @param array $config
     */
    public function __construct(array $config = [])
    {
        $this->config = array_merge($this->config, $config);
    }

    /**
     * 获取token
     */
    public function accessToken(){
        if(Cache::has('JinYeToken')){
            return Cache::get('JinYeToken');
        }else{
            $url = '/open-api/api/auth/token';
            $config = $this->config;
            $config['tkey'] = 'thirdApi'.date('YmdHis');
            $options = [
                'query'=>$config,
            ];
            $response =  $this->http($url,$options,'GET');
            if($response['code'] == 200){
                Cache::set('JinYeToken',$response['data']['accessToken'],$response['data']['expiresIn']-100);
                return $response['data']['accessToken'];
            }else{
                $this->errMsg = $response['msg'];
                return false;
                
            }
        }
    }
    /**
     * 获取网络模块
     * "deviceId" => 3266
     * "name" => "1"
     * "uid" => "000000030965"
     */
    public function hublist(){
        $url = '/open-api/api/monitor/hublist';
        $params = [
            'pageNo'=>1,
            'pageSize'=>100,
        ];
        $options = [
            'headers'=>['token'=>$this->accessToken()],
            'query'=>$params,
            
        ];
        $response =  $this->http($url,$options,'GET');
        dump($response);die();
    }
    /***
     * 获取模块状态
     * "rtuId" => 5611,5612
        "rtuType" => "1"
        "rtuAddr" => "3","4"
        "rtuName" => "智慧灯控"
        "channelCount" => 8
        "onlineStatus" => "1"
        "deviceUid" => "000000030965"
     */
    public function rtulist(){
        $url = '/open-api/api/monitor/rtulist';
        $params = [
            'deviceId'=>3266,
            'pageNo'=>1,
            'pageSize'=>100,
        ];
        $options = [
            'headers'=>['token'=>$this->accessToken()],
            'query'=>$params,
            
        ];
        $response =  $this->http($url,$options,'GET');
        dump($response);die();
    }
    /***
     * 获取模块回路状态
     * "id" => 67323
     * "channel" => 6
     * "name" => "回路6"
     * */
    public function channels(string $rtuId='5611'){
        $url = '/open-api/api/monitor/channels';
        $params = [
            'rtuId'=>$rtuId
        ];
        $options = [
            'headers'=>['token'=>$this->accessToken()],
            'query'=>$params,
            
        ];
        $response =  $this->http($url,$options,'GET');
        if($response !==false){
            return $response['data'];
        }
        return $response;
    }

    /**
     * 网络模块总开总关
     * 
     */
    public function hubSwitch( string $state = 'ON'){
        $url = '/open-api/api/cmd/hub/switch';
        $params = [
            'switchStatus'=>$state,
            'uid'=>'000000030965',
            'uids'=>[],
        ];
        $options = [
            'headers'=>['token'=>$this->accessToken(),'Content-Type' => 'application/json'],
            'json' => $params
        ];
        $response =  $this->http($url,$options);
        return $response;
    }

    /**
     * 模块总开总关
     * /open-api/api/cmd/rtu/switch
     * @PARAM $rtuAddr=3,4
     */
    public function rtuSwitch( string $rtuAddr = '3', string $state = 'ON'){
        $url = '/open-api/api/cmd/hub/switch';
        $params = [
            'switchStatus'=>$state,
            'deviceUid'=>'000000030965',
            'rtuAddr'=>$rtuAddr,
        ];
        $options = [
            'headers'=>['token'=>$this->accessToken(),'Content-Type' => 'application/json'],
            'json' => $params
        ];
        $response =  $this->http($url,$options);
        return $response;
    }
    /**
     * 模块回路开关
     * /open-api/api/cmd/rtu/switch
     * @PARAM $rtuAddr=3,4
     */
    public function channelSwitch( string $channel='1', string $rtuAddr = '3', string $state = 'ON'){
        $url = '/open-api/api/cmd/channel/switch';
        $params = [
            'switchStatus'=>$state,
            'channel'=>$channel,
            'deviceUid'=>'000000030965',
            'rtuAddr'=>$rtuAddr,
        ];
        $options = [
            'headers'=>['token'=>$this->accessToken(),'Content-Type' => 'application/json'],
            'json' => $params
        ];
        $response =  $this->http($url,$options);
        return $response;
        
    }

    public function http($url,$options=[],$method='POST'){
        try {
            $client = new Client();
            $option = [
                'timeout'         => 30,
                'connect_timeout' => 30,
                'verify'          => false,
                'http_errors'     => true,
                
            ];
            $response = $client->request($method, $this->url.$url,array_merge($option,$options));
            //dump($response->getHeaders());
            if($response->getStatusCode() == 200){
                return json_decode($response->getBody()->getContents(),1);
                
            }else{
                return false;
            }
        } catch (\Throwable $th) {
            $this->errMsg = $th->getMessage();
            return false;
        }
    }
}